package socket;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.HashMap;

import utils.MyConstaints;
import utils.ToastUtils;

/**
 * Created by Administrator on 2017/3/13.
 */

public class SocketRunnable implements Runnable {
    private Socket socket;
    private Handler handler;
    public  Handler revHandler;
    private static BufferedReader br;
    private static BufferedWriter bw;
    private HashMap<String,Integer> commend=new HashMap<>();
    public SocketRunnable(Handler handler) {
        this.handler=handler;
        commend.put("login",MyConstaints.login);
        commend.put("system",MyConstaints.system);
        commend.put("regist",MyConstaints.regist);
        commend.put("receive",MyConstaints.receive);
        commend.put("get",MyConstaints.get);
        commend.put("tip",MyConstaints.tip);
        commend.put("changepwd",MyConstaints.changepwd);
        commend.put("talking",MyConstaints.talking);
        commend.put("request",MyConstaints.request);
    }

    @Override
    public void run() {
        try {
            socket=new Socket("121.42.161.20",2345);
            br=new BufferedReader(new InputStreamReader(socket.getInputStream()));
            bw=new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
            new Thread(){
                @Override
                public void run() {
                    super.run();
                    String content=null;
                    try {
                        while (!(content=br.readLine()).equals("")){
                            int index=content.indexOf("#");
                            if (index<0)continue;
                            String key=content.substring(0,index);
                            String json=content.substring(index+1);
                            System.out.println(json);
                            Message msg=new Message();
                            msg.what= commend.get(key);
                            msg.obj=json;
                            handler.sendMessage(msg);
                        }
                    } catch (IOException e) {
                    }
                }
            }.start();
            Looper.prepare();
            revHandler=new Handler(){
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    if (msg.what== MyConstaints.send){
                        try {
                            System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++"+msg.obj.toString());
                            bw.write(msg.obj.toString()+"\r\n");
                            System.out.println(msg.obj);
                            bw.flush();
                        } catch (IOException e) {

                        }
                    }
                }
            };
            Looper.loop();

        } catch (IOException e) {
            e.printStackTrace();
            Message msg=new Message();
            msg.what= MyConstaints.socket_errer;
            msg.obj="socket连接失败";
            handler.sendMessage(msg);
        }
    }
}
